<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
// import resizeMixins from "@/util/resizeMixins.js";
export default {
  // mixins: [resizeMixins],
  props: {
    data: {
      type: Array,
      default: () => []
    },
    className: {
      type: String,
      default: "chart"
    },
    id: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "100%"
    },
    chartData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      chart: null
    };
  },
  mounted() {
    this.chart = this.createChart();
  },
  watch: {
    chartData: {
      handler(newVal) {
        this.$nextTick(() => {
          this.renderChart(newVal);
        });
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    createChart() {
      return echarts.init(document.getElementById(this.id));
    },
    renderChart(data) {
      // 根据数据渲染echart图表
      const option = this._createChartOption(data);
      if (this.chart) {
        this.chart.setOption(option);
      }
    },
    _createChartOption() {
      const option = {
        title: {
          text: "总车次",
          left: "center",
          top: "39%",
          textStyle: {
            color: "#fff",
            fontWeight: 500,
            fontSize: 13
          }
        },
        series: [
          {
            type: "gauge",
            startAngle: 180,
            endAngle: 0,
            min: 0,
            max: 50000,
            progress: {
              show: true,
              width: 18,
              itemStyle: {
                color: "#77DE73"
              }
            },
            axisLine: {
              lineStyle: {
                width: 18,
                color: [[1, "#3286FB"]]
              }
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false
            },
            axisLabel: {
              show: false
            },
            anchor: {
              show: false
            },
            title: {
              show: false
            },
            pointer: {
              show: false
            },
            detail: {
              valueAnimation: true,
              fontSize: 29,
              color: "#80E3F8",
              fontWeight: 500
              // offsetCenter: [0, "50%"]
            },
            data: [
              {
                value: 26239
              }
            ]
          }
        ]
      };
      return option;
    },
    beforeDestroy() {
      this.chart.dispose();
      this.chart = null;
    }
  }
};
</script>

<style></style>
